/**
 * Created by wei on 17/11/23.
 */


import React, { Component } from 'react';
import axios from 'axios';
import base from '../base.js';
import DivShow from '../front/divshow';
import Users from './user';
import '../persion/person.css'
const basePath = base().basePath;

class Content extends  Component{

    constructor(props){
        super(props);
        this.state = {
            loginVisible: true,
            username:'',
            password:'',
            curTab:'user',
            dataSource:[],
            pageInfo: {
                pageNumber:0,
                pageSize:50,
                totalPages:0,
                totalElements:0,
                last:true,
                first:false,
                size:0,
                orderField:'id',
                orderType:'DESC',
                username:'',
                nickname:'',
                source:0,
            },
        }
    }

    close(){

    }

    usernameChangeHandle(e){
        this.setState({
            username:e.target.value
        });

    }

    passwordChangeHandle(e){
        this.setState({
            password:e.target.value
        });
    }

    login(){
        var json = {};
        json.username = this.state.username;
        json.password = this.state.password;

        axios.post(basePath+"/admin/login", json).then((res) => {
            if(res.data.result === 'SUCCESS'){
                console.log("success");
                this.setState({
                    loginVisible: false,
                });
            }else{
                console.log("failed")
            }
        });
    }


    showContent(v){

        switch (v){
            case 'user':
                this.getData(basePath+'/user/u', v);
                break;
            case 'asu':
                this.getData(basePath+'/asu/c', v);
                break;
            case 'content':
                this.getData(basePath+'/topic/t', v);
                break;

        }
    }


    refresh(v, u){
        var url;
        if(u === void(0) || null === u){
            switch (this.state.curTab){
                case 'user':
                    url = basePath+'/user/u';
                    break;
                case 'asu':
                    url = basePath+'/asu/c';
                    break;
                case 'content':
                    url = basePath+'/topic/t';
                    break;

            }
        }else{
            url = u;
        }
        axios.post(url , this.state.pageInfo, {headers: { token: this.state.token }
        })
            .then((res) => {
                if(res.status == 200){
                    var pageInfo = this.state.pageInfo;
                    var dataSource = res.data.content;
                    pageInfo.totalPages = res.data.totalPages;
                    pageInfo.totalElements = res.data.totalElements;
                    pageInfo.last = res.data.last;
                    pageInfo.first = res.data.first;
                    pageInfo.size = res.data.size;
                    pageInfo.pageSize = res.data.size;
                    pageInfo.pageNumber = res.data.number;
                    this.setState({
                        dataSource: dataSource,
                        pageInfo: pageInfo,
                        curTab: v,
                    });
                }
            });
    }



    getData(url, v){
        this.state.pageInfo.pageNumber=0;
        this.refresh(v, url);
    }

    fnPageLoad(num){
        this.state.pageInfo.pageNumber = num;
        this.refresh(this.state.curTab);
    }

    fnUserQueryReload(username,nickname,source){
        this.state.pageInfo.nickname = nickname;
        this.state.pageInfo.username = username;
        this.state.pageInfo.source = source;
        this.refresh(this.state.curTab);
    }


    contentTab(){
        switch (this.state.curTab){
            case 'user':
                return (
                    <Users fnQueryReload={this.fnUserQueryReload.bind(this)} fnPageLoad={this.fnPageLoad.bind(this)} dataSource={this.state.dataSource} pageInfo={this.state.pageInfo} />
                );
                break;
            case 'asu':
                return (
                    <div>asu</div>
                );
                break;
            case 'content':
                return (
                    <div>content</div>
                );
                break;

        }
    }



    render(){

        let content;
        if(!this.state.loginVisible){
            content = (
                <div className="border1px" style={{padding:'10px 10px',margin: '5px 5px'}}>
                    <div style={{width:'100%', overflow:'hidden'}}>
                        <div className="float_l borderBh" style={{width:'210px',height:'41px'}}>&nbsp;</div>
                        <div className={this.state.curTab == 'user' ? 'float_l border1px borderBn' : 'float_l border1px'} style={{width:'100px', height:'40PX',lineHeight:'40px'}}>
                                    <span style={{marginLeft:'10px'}}>
                                        <a onClick={this.showContent.bind(this,'user')} className="btn2" href="javascript:;">注册用户</a>
                                    </span>
                        </div>
                        <div className="float_l borderBh" style={{width:'10px',height:'41px'}}>&nbsp;</div>
                        <div className={this.state.curTab == 'asu' ? 'float_l border1px borderBn' : 'float_l border1px'} style={{width:'100px', height:'40PX',lineHeight:'40px'}}>
                                    <span style={{marginLeft:'10px'}}>
                                        <a onClick={this.showContent.bind(this,'asu')} className="btn2" href="javascript:;">举报内容</a>
                                    </span>
                        </div>
                        <div className="float_l borderBh" style={{width:'10px',height:'41px'}}>&nbsp;</div>
                        <div className={this.state.curTab == 'content' ? 'float_l border1px borderBn' : 'float_l border1px'} style={{width:'100px', height:'40PX',lineHeight:'40px'}}>
                                    <span style={{marginLeft:'10px'}}>
                                        <a  onClick={this.showContent.bind(this,'content')} className="btn2" href="javascript:;">内容</a>
                                    </span>
                        </div>
                        <div className="float_l borderBh" style={{width:'350px',height:'41px'}}>&nbsp;</div>
                    </div>
                    <div style={{marginTop:'10px'}}>
                        {this.contentTab()}
                    </div>
                </div>
            );
        }

        return (
            <div className="content mid wi90">
                <DivShow visible={this.state.loginVisible} close={this.close.bind(this)}>
                    <div className="mid" style={{height:'30px', width:'200px'}}>
                        <input size="30" onChange={this.usernameChangeHandle.bind(this)} className="input" type="text" placeholder="用户名" value={this.state.username}/>
                    </div>
                    <div className="mid" style={{height:'30px', width:'200px'}}>
                        <input size="30" onChange={this.passwordChangeHandle.bind(this)}  className="input" type="password" placeholder="密码" value={this.state.password}/>
                    </div>
                    <div className="mid" style={{height:'30px', width:'200px'}}>
                        <button onClick={this.login.bind(this)}>登陆</button>
                    </div>
                    <div style={{height:'20px'}}>

                    </div>
                </DivShow>
                {content}
            </div>
        );
    }

}

export default Content;
